package com.qk.management.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.domain.course.CourseDO;
import com.qk.dto.course.CourseDTO;
import com.qk.entity.Course;
import com.qk.management.mapper.CourseMapper;
import com.qk.management.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;

    @Override
    public Result list(CourseDTO courseDTO) {
        PageHelper.startPage(courseDTO.getPage(), courseDTO.getPageSize());
        List<CourseDO> list = courseMapper.selectByPage(courseDTO);
        Page<CourseDO> page = (Page<CourseDO>) list;
        PageResult pageResult = PageResult.<CourseDO>builder()
                .total(Math.toIntExact(page.getTotal()))
                .rows(page)
                .build();
        return Result.success(pageResult);
    }

    @Override
    public Course get(Integer id) {
        return courseMapper.selectById(id);
    }

    @Override
    public void add(Course course) {
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.insert(course);
    }

    @Override
    public void delete(Integer id) {
        courseMapper.deleteById(id);
    }


    @Override
    public List<Course> selectAll() {
        return courseMapper.selectAll();
    }

    @Override
    public void updateById(Course course) {
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.updateById(course);
    }

    @Override
    public List<Course> selectBySubject(Integer subject) {
        return courseMapper.selectBySubject(subject);
    }
}
